package com.robusta.cityuate.objects;

import java.io.Serializable;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.robusta.cityuate.utils.TimeUtils;

@SuppressWarnings("serial")
public class TimeSlot implements Serializable {

	public static final String SLOT_START = "start_time";
	public static final String SLOT_END = "end_time";
	public static final String TIME_SLOT = "time_slot";
	public static final String SLOT_RESERVATION = "reservation";
	public static final String SLOT_ID = "id";
	private static final String SLOT_IS_RESERVED = "reserved";
	

	Date startTime;
	Date endTime;
	Reservation reservation;
	private long id;
	private Offering offer;
	private boolean isReserved;

	public boolean isReserved() {
		return isReserved;
	}

	public void setReserved(boolean isReserved) {
		this.isReserved = isReserved;
	}

	public TimeSlot(Offering offer) {
		this.offer = offer;
	}

	public Offering getOffer() {
		return offer;
	}

	public void setOffer(Offering offer) {
		this.offer = offer;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Reservation getReservation() {
		return reservation;
	}

	public void setReservations(Reservation reservations) {
		this.reservation = reservations;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public static TimeSlot getTimeSlotfromJSON(JSONObject slotJSON,
			Offering offer) {
		TimeSlot slot = new TimeSlot(offer);
		try {
			JSONObject slotData = slotJSON.getJSONObject(TIME_SLOT);
			slot.id = slotData.getLong(SLOT_ID);
			slot.startTime = TimeUtils.getDateTimeFromString(
					slotData.getString(SLOT_START), Offering.DATE_TIME_FORMAT);
			slot.endTime = TimeUtils.getDateTimeFromString(
					slotData.getString(SLOT_END), Offering.DATE_TIME_FORMAT);
			if (slotData.has(SLOT_RESERVATION)&&!slotData.isNull(SLOT_RESERVATION))
				slot.reservation = Reservation.fromJSON(slotData);
			slot.isReserved=slotData.getBoolean(SLOT_IS_RESERVED);
			return slot;
		} catch (JSONException e) {
			e.printStackTrace();

		}
		return null;
	}

}
